<?php

    require_once Core_IO_Path::combine(SERUM_LIB_PATH, 'Securimage', 'securimage.php');

    /**
     * @package         Captcha
     * @subpackage      Adapter
     *
     * @license         GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Captcha_Adapter_Securimage extends Captcha_Adapter_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
    //                            Attributes                                  //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Singleton
         * 
         * @var     Securimage
         */
        private $_instance = null;

    ////////////////////////////////////////////////////////////////////////////
    //                            Methods                                     //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         */
        public function __construct()
        {
            $this->_instance = new Securimage();
            $this->setImageType(self::IMAGE_JPEG);
            $this->setTtf(Core_IO_Path::combine(SERUM_LIB_PATH, 'Securimage', 'elephant.ttf'));
        }

        /**
         * Shows
         *
         * @return      string
         */
        public function show()
        {
            switch ($this->_type) {
                case self::IMAGE_JPEG:
                    $this->_instance->image_type = SI_IMAGE_JPEG;
                    break;

                case self::IMAGE_GIF:
                   $this->_instance->image_type = SI_IMAGE_GIF;
                    break;

                case self::IMAGE_PNG:
                    $this->_instance->image_type = SI_IMAGE_PNG;
                    break;
            }

            $this->_instance->image_width   = $this->_width;
            $this->_instance->image_height  = $this->_height;
            $this->_instance->code_length   = $this->_length;
            $this->_instance->charset       = $this->_charset;
            $this->_instance->use_wordlist  = false;

            if ($this->_ttf !== null) {
                $this->_instance->ttf_file      = $this->_ttf;
            }

            $captchaDir = Core_IO_Path::combine(PROJECT_MEDIA_PATH, 'captcha');

            if (!Core_IO_Directory::exists($captchaDir)) {
                Core_IO_Directory::createDirectory($captchaDir);
            }

            // Writes file
            $uniqueId       = self::uniqueId();
            $captchaFile    = Core_IO_Path::combine($captchaDir, $uniqueId) .'.'. $this->_type;
            $this->_instance->file_name = $captchaFile;

            if ($this->_background !== null) {
                $this->_instance->show($this->_background);
            } else {
                $this->_instance->show();
            }

            $registry = Zend_Registry::getInstance();
            return $registry['config']->urlengine->basePath . 'project/media/captcha/'. $uniqueId .'.'. $this->_type;
        }

        /**
         * Checks result
         *
         * @param       string      $code
         * @return      boolean
         */
        public function check($code)
        {
            return $this->_instance->check($code);
        }
    }